//
//  PageTileView.swift
//  CWLiving
//
//  Created by 方正刚 on 2017/7/5.
//  Copyright © 2017年 方正刚. All rights reserved.
//

import UIKit


/// 代理协议
protocol PageTileViewDelegate : class {
    
    func pagetitleView(tittleVeiw: PageTileView, selectIndex: Int)
    
}


/// 类私有常亮
private let SCROLLLINE_HEIGHT : CGFloat = 2.0

private let NORMOL_COLOR: (CGFloat, CGFloat, CGFloat) = (85, 85, 85)
private let SELECTED_COLOR: (CGFloat, CGFloat, CGFloat) = (255, 128, 0)

class PageTileView: UIView {
    
    /// 定义属性
    var titles: [String]
    
    var currentIndex: Int = 0
    
    /// 代理属性
    weak var delegate: PageTileViewDelegate?
    
    lazy var titleLabels : [UILabel] = [UILabel]()
    /// 闭包函数懒加载 scrollView
    lazy var scrollView : UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        return scrollView
    }()
    
    
    /// 滑块
    lazy var scrollLine : UIView = {
        let scrollLine = UIView()
       scrollLine.backgroundColor = UIColor.orange
        return scrollLine
    }()
    
    /// 自定义构造函数
    ///
    /// - Parameters:
    ///   - frame: frame
    ///   - titles: 标题数组
    init(frame: CGRect, titles: [String]) {
        
        self.titles = titles
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}



extension PageTileView {
    
    func setupUI() {
        addSubview(scrollView)
        scrollView.frame = bounds
        
        setupTitles()
        
        setupBottomLine()
    }
    
    
    /// 标签
    private func setupTitles() {
        
        let labelW : CGFloat = frame.width / CGFloat(titles.count)
        let labelH : CGFloat = frame.height - CGFloat(SCROLLLINE_HEIGHT)
        let labelY : CGFloat = 0
        
        for (index, title) in titles.enumerated() {
            
            let label = UILabel()
            
            label.text = title
            label.tag = index
            label.font = UIFont.systemFont(ofSize: 15)
            label.textColor = UIColor(r: NORMOL_COLOR.0, g: NORMOL_COLOR.1, b: NORMOL_COLOR.2)
            label.textAlignment = .center
           
            let labelX : CGFloat = labelW * CGFloat(index)
            
            label.frame = CGRect(x: labelX, y: labelY, width: labelW, height: labelH)
            
            scrollView.addSubview(label)
            
            titleLabels.append(label)
            
            label.isUserInteractionEnabled = true
            
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.titleLabelClick(tapGesture:)))
            
            label.addGestureRecognizer(tapGesture)
            
        }
        
    }
    
    
    /// 底部的分割线和滑块
    private func setupBottomLine() {
        
        /// 底部分割线
        let bottomLine = UIView()
        bottomLine.backgroundColor = UIColor.lightGray
        let lineH : CGFloat = 0.5
        bottomLine.frame = CGRect(x: 0, y: frame.height - lineH,width: frame.width, height: lineH)
        addSubview(bottomLine)
        
        guard let fristLabel = titleLabels.first else {
            return
        }
        
        fristLabel.textColor = UIColor(r: SELECTED_COLOR.0, g: SELECTED_COLOR.1, b: SELECTED_COLOR.2)
        
        scrollView.addSubview(scrollLine)
        
        scrollLine.frame = CGRect(x: fristLabel.frame.origin.x, y: frame.height - SCROLLLINE_HEIGHT, width: fristLabel.frame.width, height: SCROLLLINE_HEIGHT)
    }
    
}


// MARK: - 处理label的点击事件
extension PageTileView {
  func titleLabelClick(tapGesture: UITapGestureRecognizer) {
    
    guard let currentLabel = tapGesture.view as? UILabel else { return }
    
    let oldLabel = titleLabels[currentIndex]
    
    oldLabel.textColor = UIColor(r: NORMOL_COLOR.0, g: NORMOL_COLOR.1, b: NORMOL_COLOR.2)
    
    currentLabel.textColor = UIColor(r: SELECTED_COLOR.0, g: SELECTED_COLOR.1, b: SELECTED_COLOR.2)
    
    currentIndex = currentLabel.tag
    
    // 改变滑块的位置
    
    let scrollLineX = CGFloat(currentIndex) * scrollLine.frame.width
    
    UIView.animate(withDuration: 0.15) { 
        self.scrollLine.frame.origin.x = scrollLineX
    }
    
    
    // 响应代理方法
    delegate?.pagetitleView(tittleVeiw: self, selectIndex: currentIndex)
    
  }
    
}

extension PageTileView {
    
    func setTitleWithProgress(progress: CGFloat,targetIndex:Int, sourceIndex: Int) {
        
        let sourceLabel = titleLabels[sourceIndex]
        
        let targetLabel = titleLabels[targetIndex]
        
        
        
        //处理滑块
        
        let moveTotalX = targetLabel.frame.origin.x - sourceLabel.frame.origin.x
        
        let moveX = moveTotalX * progress
        
        scrollLine.frame.origin.x = sourceLabel.frame.origin.x + moveX
        
        // label颜色的渐变
        // 取出变化范围
        let colorDelta = (SELECTED_COLOR.0 - NORMOL_COLOR.0, SELECTED_COLOR.1 - NORMOL_COLOR.1, SELECTED_COLOR.2 - NORMOL_COLOR.2)
        //变化的sourceLabel
        
        let sourceR = SELECTED_COLOR.0 - (colorDelta.0 * progress)
        let sourceG = SELECTED_COLOR.1 - (colorDelta.1 * progress)
        let sourceB = SELECTED_COLOR.2 - (colorDelta.2 * progress)
        sourceLabel.textColor = UIColor(r: sourceR, g: sourceG, b: sourceB)
        
        //变化的targetLabel
        let targetR = NORMOL_COLOR.0 + (colorDelta.0 * progress)
        let targetG = NORMOL_COLOR.1 + (colorDelta.1 * progress)
        let targetB = NORMOL_COLOR.2 + (colorDelta.2 * progress)
        targetLabel.textColor = UIColor(r: targetR, g: targetG, b: targetB)

        // 更新当前的item的下标
        currentIndex = targetIndex
        
    }
}


